package com.example.walkingtour;

import com.example.walkingtour.list.TourList;
import com.example.walkingtour.maps.MyMapFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

public class TabManager extends Fragment implements OnTabChangeListener {

	public static final String TAG = "TabManager";
	public static final String TAB_LIST = "list";
	public static final String TAB_MAP = "map";

	private static Fragment tourList, tourMap;

	private View mRoot;
	private TabHost mTabHost;
	private int mCurrentTab = 0;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	/**
	 * Responds to tab changes, and changes the tab accordingly
	 */
	@Override
	public void onTabChanged(String tabId) {
		Log.d(TAG, "onTabChanged(): tabId=" + tabId);
		if (TAB_LIST.equals(tabId)) {
			updateTab(tabId, R.id.listTab);
			mCurrentTab = 0;
		}
		if (TAB_MAP.equals(tabId)) {
			updateTab(tabId, R.id.mapTab);
			mCurrentTab = 1;
		}

		Log.d(TAG, "onTabChanged()");
	}

	/**
	 * When this Fragment is loaded, it sets up the tabs and creates the tab
	 * widget
	 * 
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.d(TAG, "onCreateView()");
		mRoot = inflater.inflate(R.layout.tab_manager, null);
		mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost);
		setupTabs();
		return mRoot;
	}

	/**
	 * sets up the tabhost with the List & map fragments
	 */
	private void setupTabs() {
		Log.d(TAG, "setupTabs()");
		mTabHost.setup(); // important!
		mTabHost.addTab(newTab(TAB_LIST, R.string.listView, R.id.listTab));
		mTabHost.addTab(newTab(TAB_MAP, R.string.mapView, R.id.mapTab));
		// mTabHost.addTab(newTab(TAB_MAP, R.string.app_name, R.id.mapTab));
	}

	/**
	 * Creates a TabSpec for loading to the tab host
	 * 
	 * @param tag
	 *            String id to identify the tab
	 * @param labelId
	 *            integer that identifies the string to display in the tab
	 * @param tabContentId
	 *            integer that identifies the Frame to place in the tab
	 * @return
	 */
	private TabSpec newTab(String tag, int labelId, int tabContentId) {
		Log.d(TAG, "buildTab(): tag=" + tag);

		View indicator = LayoutInflater.from(getActivity()).inflate(
				R.layout.tab_heading,
				(ViewGroup) mRoot.findViewById(android.R.id.tabs), false);
		((TextView) indicator.findViewById(R.id.tabHeading)).setText(labelId);

		TabSpec tabSpec = mTabHost.newTabSpec(tag);
		tabSpec.setIndicator(indicator);
		tabSpec.setContent(tabContentId);
		Log.d(TAG, "newTab()");
		return tabSpec;
	}

	/**
	 * when activity is created, initialize the tourList & tourMap fragments
	 */
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		Log.d(TAG, "onActivityCreated()");
		super.onActivityCreated(savedInstanceState);
		setRetainInstance(true);

		tourList = new TourList();
		// tourMap = new TourMap();
		tourMap = new MyMapFragment();

		mTabHost.setOnTabChangedListener(this);
		mTabHost.setCurrentTab(mCurrentTab);
		// manually start loading stuff in the first tab
		updateTab(TAB_LIST, R.id.listTab);
	}

	/**
	 * Updates the screen to display the fragment that corresponds to the
	 * selected Tab
	 * 
	 * @param tabId
	 *            String id of newly selected tab
	 * @param placeholder
	 *            integer that identifies the Frame to place in the tab
	 */
	private void updateTab(String tabId, int placeholder) {
		Log.d(TAG, "updateTab()");
		FragmentManager fm = getFragmentManager();
		if (fm.findFragmentByTag(tabId) == null) {
			if (tabId.equals(TabManager.TAB_LIST)) {
				fm.beginTransaction().replace(placeholder, tourList, tabId)
						.commit();
			} else if (tabId.equals(TabManager.TAB_MAP)) {
				fm.beginTransaction().replace(placeholder, tourMap, tabId)
						.commit();

			}
		}
	}

}
